import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";
import { EUI_Style } from "./EUI_Style.js";
import { EUI_Window } from "./EUI_Window.js";

export class EUI_Dialog_Msgbox extends tt2.QUI_Container {
    constructor() {
        super();
        this.localRect.setAsFill();

        //Back
        let back = EUI_Style.CreateWhiteImage();
        back.color = new tt.Color(0, 0, 0, 0.5);
        this.addChild(back);


        let border = EUI_Style.CreateDefaultBack();
        border.localRect.setHPosFill(200, 200);
        border.localRect.setVPosFill(200, 200);
        this.addChild(border);

        let overlay = new tt2.QUI_Overlay();
        overlay.localRect.setAsFill();
        border.addChild(overlay);
        //标签
        this._label = new tt2.QUI_Label(EUI_Style.font);
        this._label.localRect.setHPosByCenter(200);
        this._label.localRect.setVPosByCenter(20);
        border.addChild(this._label);



        {
            //OK按钮
            let btn = this._btnOk = EUI_Style.CreateButton("确定");
            btn.localRect.setHPosByCenter(100);
            btn.localRect.setVPosByCenter(16);
            btn.localRect.offsetY1 = 20;
            btn.localRect.offsetY2 = 36;
            border.addChild(btn);
            btn.OnClick = () => {
                if (this._win.GetPopTag() == this._tag) {
                    this._isShow = false;
                    this._win.PopModel();
                }
            }
        }
        {
            //Yes按钮
            let btn = this._btnYes = EUI_Style.CreateButton("是");
            btn.localRect.setHPosByLeftBorder(80, 250);
            btn.localRect.setVPosByCenter(16);
            btn.localRect.offsetY1 = 20;
            btn.localRect.offsetY2 = 36;
            border.addChild(btn);
            btn.OnClick = () => {
                if (this._win.GetPopTag() == this._tag) {
                    this._yes = true;
                    this._isShow = false;
                    this._win.PopModel();
                }
            }
        }
        {
            //No按钮
            let btn = this._btnNo = EUI_Style.CreateButton("否");
            btn.localRect.setHPosByRightBorder(80, 250);
            btn.localRect.setVPosByCenter(16);
            btn.localRect.offsetY1 = 20;
            btn.localRect.offsetY2 = 36;
            border.addChild(btn);
            btn.OnClick = () => {
                if (this._win.GetPopTag() == this._tag) {
                    this._isShow = false;
                    this._win.PopModel();
                }
            }
        }
    }
    private _InitMsg(msg: string, yesno: boolean) {
        this._label.text = msg;
        this._yes = false;
        if (yesno) {
            this._btnYes.Enable = true;
            this._btnNo.Enable = true;
            this._btnOk.Enable = false;
        }
        else {
            this._btnYes.Enable = false;
            this._btnNo.Enable = false;
            this._btnOk.Enable = true;
        }
    }
    private _btnOk: tt2.QUI_Button;
    private _btnYes: tt2.QUI_Button;
    private _btnNo: tt2.QUI_Button;
    private _label: tt2.QUI_Label;
    private _isShow: boolean;
    private _win: EUI_Window
    private _yes: boolean;
    readonly _tag: string = "_dialog_msgbox_";
    async Show(win: EUI_Window, msg: string, yesno: boolean = false): Promise<boolean> {
        this._isShow = true;
        this._win = win;
        this._InitMsg(msg, yesno);

        win.PushModel(this, this._tag, () => {
            if (this._win.GetPopTag() == this._tag) {
                this._isShow = false;
                this._win.PopModel();
            }
        });

        while (this._isShow) {
            await tt.sleep(1);
        }
        return this._yes;
    }

}